

package ro.ase.catalog.security.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import ro.ase.catalog.beans.*;
import ro.ase.catalog.beans.dto.TeacherDisciplineStudentsDTO;
import ro.ase.catalog.security.dao.IAuthorityDAO;
import ro.ase.catalog.security.dao.IStudentDAO;
import ro.ase.catalog.security.dao.ITeacherDAO;
import ro.ase.catalog.security.service.ILoginService;
import ro.ase.catalog.security.service.ITeacherDisciplineClassService;


import java.util.*;

public class LoginServiceImpl implements ILoginService {
	
    @Autowired
    private IAuthorityDAO authorityDAO;

    @Autowired
    private ITeacherDAO teacherDAO;

    @Autowired
    private IStudentDAO studentDAO;

    @Autowired
    private ITeacherDisciplineClassService teacherDisciplineClassService;

    public Set<Map<String, Object>> loadChildNodes(User user, String node, Teacher teacherLogged) {
        Set<Map<String, Object>> menu = new HashSet<Map<String, Object>>();

        Set<Role> roles = user.getRoles();
        for (Role role : roles) {
            List<Authority> auths = authorityDAO.findByRoleId(role.getId());

            for (Authority auth : auths) {

                if (StringUtils.indexOf(auth.getName(), "/") >= 0) {
                    String tmp = "";

                    if ("allModulesRoot".equals(node)) {
                        tmp = auth.getName();

                    } else if (StringUtils.indexOf(auth.getName(), node+"/") >= 0) {
                        tmp = StringUtils.substringAfter(auth.getName(), node+"/");
                    } else
                        continue;

                    Map<String, Object> map = new HashMap<String, Object>();
                    String[] names = StringUtils.split(tmp, "/");
                    map.put("text", names[0]);
                    map.put("id", "/" + names[0]);
                    if (names.length > 1) {

                        map.put("url", names[0]);
                        map.put("leaf", false);
                    } else {

                        map.put("url", StringUtils
                                .substring(auth.getValue(), 1,auth.getValue().length()-1));
                        map.put("leaf", true);
                    }
                    menu.add(map);
                }
            }
            studentTree(user, node, menu, role);
            teacherTree(node, teacherLogged, menu, role);
        }

        return menu;
    }

    private void teacherTree(String node, Teacher teacherLogged, Set<Map<String, Object>> menu, Role role) {
        if(role.getName().equals("ROLE_TEACHER")) {
            if ("allModulesRoot".equals(node)) {
            Map<String, Object> map = new HashMap<String, Object>();
            String names = "Clase";
            map.put("text", names);
            map.put("id", "/" + names);
            map.put("leaf", false) ;
            menu.add(map);
                if(teacherLogged.getClassMaster()!=null){
                    if ("allModulesRoot".equals(node)) {
                        Map<String, Object> mapTemp = new HashMap<String, Object>();
                        String namesTemp = "Diriginte";
                        mapTemp.put("text", namesTemp);
                        mapTemp.put("id", "/" + namesTemp);
                        mapTemp.put("leaf", false) ;
                        menu.add(mapTemp);

                        Map<String, Object> mapTemp2 = new HashMap<String, Object>();
                        String namesTemp2 = "Medii Generale";
                        mapTemp2.put("text", namesTemp2);
                        mapTemp2.put("id", "/" + namesTemp2);
                        mapTemp2.put("leaf", false) ;
                        menu.add(mapTemp2);
                    }

                }
            }
            if("/Clase".equals(node)){
            List<TeacherDisciplineClass> teacherDisciplineClasses=teacherDisciplineClassService.getAllClassByTeacher(teacherLogged);
            for(int i=0;i<teacherDisciplineClasses.size();i++){

                    Map<String, Object> mapClass = new HashMap<String, Object>();
                    String className = teacherDisciplineClasses.get(i).getaSchoolClass().getName();
                    String url= "catalog/teacherCatalog.jsp?classId="+ teacherDisciplineClasses.get(i).getaSchoolClass().getId();
                    mapClass.put("text", className+" "+teacherDisciplineClasses.get(i).getDiscipline().getName());
                    mapClass.put("id", "/" + className+" "+teacherDisciplineClasses.get(i).getDiscipline().getName());
                    mapClass.put("url", url);
                    mapClass.put("leaf", true);

                    menu.add(mapClass);

                }

            }
            if("/Diriginte".equals(node)){

                    List<TeacherDisciplineStudentsDTO> studentsDTOs=studentDAO.findAllStudentsByClass(teacherLogged, teacherLogged.getClassMaster());
                    for(int i=0;i<studentsDTOs.size();i++)
                    {
                        Map<String, Object> mapClass = new HashMap<String, Object>();
                        mapClass.put("text", studentsDTOs.get(i).getLastName()+" "+studentsDTOs.get(i).getFirstName());
                        mapClass.put("id", "/" + studentsDTOs.get(i).getLastName()+" "+studentsDTOs.get(i).getFirstName());
                        mapClass.put("leaf", false);
                        menu.add(mapClass);
                    }

            }
            if("/Medii Generale".equals(node)){


                    Map<String, Object> mapClass = new HashMap<String, Object>();
                    String url= "catalog/averageClasses.js";
                    mapClass.put("text", "Medii Generale");
                    mapClass.put("id", "/" + "Medii Generale");
                    mapClass.put("url", url);
                    mapClass.put("leaf", true);
                    menu.add(mapClass);


            }
            if(!(node.equals("allModulesRoot")))   {
                if(!node.equals("/Clase"))  {
                    if(!node.equals("/Diriginte"))  {
                addDisciplines(node, teacherLogged, menu);
                addGradesAndAbsences(node, teacherLogged, menu);
                    }
                 }
            }
         }
    }

    private void addGradesAndAbsences(String node, Teacher teacherLogged, Set<Map<String, Object>> menu) {
        List<TeacherDisciplineStudentsDTO> studentsDTOs=studentDAO.findAllStudentsByClass(teacherLogged, teacherLogged.getClassMaster());
        for(int i=0;i<studentsDTOs.size();i++)
        {   Student student=studentDAO.get(studentsDTOs.get(i).getId());
            List<TeacherDisciplineClass> disciplineClasses=teacherDisciplineClassService.getAllDisciplinesByStudent(student);
            for(int j=0;j<disciplineClasses.size();j++)
            {
                if(("/" + disciplineClasses.get(j).getDiscipline().getName()+"-"+student.getId()).equals(node))
                {
                    String url="catalog/diriginteCatalogNote.jsp?studentId="+student.getId()+"&teacherId="+disciplineClasses.get(j).getTeacher().getId();
                    Map<String, Object> mapClass = new HashMap<String, Object>();
                    mapClass.put("text", "Note");
                    mapClass.put("id", "/" + "Note"+student.getId());
                    mapClass.put("url", url);
                    mapClass.put("leaf", true);
                    menu.add(mapClass);

                    String url2="catalog/diriginteCatalogAbsente.jsp?studentId="+student.getId()+"&teacherId="+disciplineClasses.get(j).getTeacher().getId();
                    Map<String, Object> mapClass2 = new HashMap<String, Object>();
                    mapClass2.put("text", "Absente");
                    mapClass2.put("id", "/" + "Absente"+student.getId());
                    mapClass2.put("url", url2);
                    mapClass2.put("leaf", true);
                    menu.add(mapClass2);
                    j= disciplineClasses.size()+1;
                    i= studentsDTOs.size()+1;
                }
            }
        }
    }

    private void addDisciplines(String node, Teacher teacherLogged, Set<Map<String, Object>> menu) {
        List<TeacherDisciplineStudentsDTO> studentsDTOs=studentDAO.findAllStudentsByClass(teacherLogged, teacherLogged.getClassMaster());
        for(int i=0;i<studentsDTOs.size();i++)
        {
            if(("/" + studentsDTOs.get(i).getLastName()+" "+studentsDTOs.get(i).getFirstName()).equals(node))
            {
                Student student=studentDAO.get(studentsDTOs.get(i).getId());
                List<TeacherDisciplineClass> disciplineClasses=teacherDisciplineClassService.getAllDisciplinesByStudent(student);
                for(int j=0;j<disciplineClasses.size();j++)
                {
                    Map<String, Object> mapClass = new HashMap<String, Object>();
                    mapClass.put("text", disciplineClasses.get(j).getDiscipline().getName());
                    mapClass.put("id", "/" + disciplineClasses.get(j).getDiscipline().getName()+"-"+student.getId());
                    mapClass.put("leaf", false);
                    menu.add(mapClass);
                }
                i= studentsDTOs.size()+1;

            }
        }
    }

    private void studentTree(User user, String node, Set<Map<String, Object>> menu, Role role) {
        if(role.getName().equals("ROLE_STUDENT"))  {
            if ("allModulesRoot".equals(node)) {
                Map<String, Object> mapTemp = new HashMap<String, Object>();
                String namesTemp = "Materii";
                mapTemp.put("text", namesTemp);
                mapTemp.put("id", "/" + namesTemp);
                mapTemp.put("url", namesTemp);
                mapTemp.put("leaf", false) ;
                menu.add(mapTemp);

                Map<String, Object> mapTemp2 = new HashMap<String, Object>();
                String namesTemp2 = "Medii generale";
                mapTemp2.put("text", namesTemp2);
                mapTemp2.put("id", "/" + namesTemp2);
                mapTemp2.put("leaf", false) ;
                menu.add(mapTemp2);
            }
            if("/Materii".equals(node)){
                Student student=studentDAO.getStudentByUserName(user.getLoginname());
                List<TeacherDisciplineClass> teacherDisciplineClasses=teacherDisciplineClassService.getAllDisciplinesByStudent(student);
                for(int i=0;i<teacherDisciplineClasses.size();i++){
                    Map<String, Object> mapClass = new HashMap<String, Object>();
                    String className = teacherDisciplineClasses.get(i).getaSchoolClass().getName();
                    mapClass.put("text", className+" "+teacherDisciplineClasses.get(i).getDiscipline().getName());
                    mapClass.put("id", "/" + className+" "+teacherDisciplineClasses.get(i).getDiscipline().getName());

                    mapClass.put("leaf", false);

                    menu.add(mapClass);
                }
            }

            if("/Medii generale".equals(node)){


                Map<String, Object> mapClass = new HashMap<String, Object>();
                String url= "catalog/averageDisciplines.js";
                mapClass.put("text", "Medii Generale");
                mapClass.put("id", "/" + "Medii Generale");
                mapClass.put("url", url);
                mapClass.put("leaf", true);
                menu.add(mapClass);


            }


                Student student=studentDAO.getStudentByUserName(user.getLoginname());
                List<TeacherDisciplineClass> disciplineClasses=teacherDisciplineClassService.getAllDisciplinesByStudent(student);
                String className = student.getSchoolClass().getName();
                for(int j=0;j<disciplineClasses.size();j++)
                {
                    if(("/" + className+" "+ disciplineClasses.get(j).getDiscipline().getName()).equals(node))
                    {
                        String url="catalog/diriginteCatalogNote.jsp?studentId="+student.getId()+"&teacherId="+disciplineClasses.get(j).getTeacher().getId();
                        Map<String, Object> mapClass = new HashMap<String, Object>();
                        mapClass.put("text", "Note");
                        mapClass.put("id", "/" + "Note"+student.getId()+" "+disciplineClasses.get(j).getId());
                        mapClass.put("url", url);
                        mapClass.put("leaf", true);
                        menu.add(mapClass);

                        String url2="catalog/elevCatalogAbsente.jsp?studentId="+student.getId()+"&teacherId="+disciplineClasses.get(j).getTeacher().getId();
                        Map<String, Object> mapClass2 = new HashMap<String, Object>();
                        mapClass2.put("text", "Absente");
                        mapClass2.put("id", "/" + "Absente"+student.getId()+" "+disciplineClasses.get(j).getId());
                        mapClass2.put("url", url2);
                        mapClass2.put("leaf", true);
                        menu.add(mapClass2);
                    }
                }
            }

    }

    public IAuthorityDAO getAuthorityDAO() {
        return authorityDAO;
    }

    public void setAuthorityDAO(IAuthorityDAO authorityDAO) {
        this.authorityDAO = authorityDAO;
    }

    public ITeacherDAO getTeacherDAO() {
        return teacherDAO;
    }

    public void setTeacherDAO(ITeacherDAO teacherDAO) {
        this.teacherDAO = teacherDAO;
    }

    public ITeacherDisciplineClassService getTeacherDisciplineClassService() {
        return teacherDisciplineClassService;
    }

    public IStudentDAO getStudentDAO() {
        return studentDAO;
    }

    public void setStudentDAO(IStudentDAO studentDAO) {
        this.studentDAO = studentDAO;
    }

    public void setTeacherDisciplineClassService(ITeacherDisciplineClassService teacherDisciplineClassService) {
        this.teacherDisciplineClassService = teacherDisciplineClassService;
    }
}
